@using DotNetNuke.Entities.Modules;
@using System.Configuration;
@using YouTubeGallery;

@{
    string connectionString = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;


    if (IsPost)
    {
        // Handle add new YouTube Guid
        if (!Request["btnAddYouTubeGuid"].IsEmpty())
        {
            string youTubeGuid = Request["txtYouTubeGuidAdd"];
            if (!youTubeGuid.IsEmpty())
            {
                using (GalleryDataContext dataContext = new GalleryDataContext(connectionString))
                {
                    YouTubeGallery newItem = new YouTubeGallery();
                    newItem.ModuleId = Dnn.Module.ModuleID;
                    newItem.YouTubeGuid = youTubeGuid;
                    dataContext.YouTubeGalleries.InsertOnSubmit(newItem);
                    dataContext.SubmitChanges();
                }
            }
            else
            {
                @:<p style="color: red;">YouTube Id is required</p>
            }
        }

        // Handle update You Tube Guid        
        if (!Request["btnUpdateYouTubeGuid"].IsEmpty())
        {
            int galleryIdToUpdate = int.Parse(Request["galleryId"]);
            string youTubeGuid = Request["txtYouTubeGuidEdit"];
            if (!youTubeGuid.IsEmpty())
            {
                using (GalleryDataContext dataContext = new GalleryDataContext(connectionString))
                {
                    YouTubeGallery itemToUpdate = dataContext.YouTubeGalleries.Where(o => o.YouTubeGalleryId == galleryIdToUpdate).First();
                    itemToUpdate.YouTubeGuid = youTubeGuid;
                    dataContext.SubmitChanges();
                }
            }
            else
            {
                @:<p style="color: red;">YouTube Id is required</p>
            }
        }        
        
        // Handle removing gallery
        int galleryIdToRemove = _FindRemoveGalleryId();
        if (galleryIdToRemove > 0)
        {
            using (GalleryDataContext dataContext = new GalleryDataContext(connectionString))
            {
                YouTubeGallery itemToDelete = dataContext.YouTubeGalleries.Where(o => o.YouTubeGalleryId == galleryIdToRemove).First();
                dataContext.YouTubeGalleries.DeleteOnSubmit(itemToDelete);
                dataContext.SubmitChanges();
            }
        }
    }


    List<YouTubeGallery> galleryList;
    using (GalleryDataContext dataContext = new GalleryDataContext(connectionString))
    {
        galleryList = dataContext.YouTubeGalleries.Where(o => o.ModuleId == Dnn.Module.ModuleID).ToList();
    }      
    
    
}

@functions{
    private int _FindRemoveGalleryId()
    {
        int galleryId = 0;
        foreach (string key in Request.Form.Keys)
        {
            if (key.StartsWith("btnRemoveId"))
            {
                galleryId = int.Parse(key.Replace("btnRemoveId", ""));
            }
        }

        return galleryId;
    } 
}    
<table>
    <tr>
        <td>YouTube Id</td>        
        <td></td>
    </tr>
    
    @foreach (YouTubeGallery item in galleryList)
    {   
    <tr>
        <td>@item.YouTubeGuid</td>        
        <td>
            <input type="button" value="Edit" onclick="ShowEditDialog(@item.YouTubeGalleryId, '@item.YouTubeGuid');" />
            <input type="submit" name="btnRemoveId@(item.YouTubeGalleryId)" value="Remove" />            
        </td>
    </tr>    
    }
</table>

<div id="dlgEditYouTubeGuid" style="display: none;">
<fieldset>
<legend><b>Edit Item</b></legend>
<p>
    <label for="text2">YouTube Id:</label>
    <input type="text" name="txtYouTubeGuidEdit" />
</p>
<p><input type="submit" name="btnUpdateYouTubeGuid" value="Update" /></p>
</fieldset>
<input type="hidden" name="galleryId" />
</div>


<fieldset>
<legend><b>Add New</b></legend>
<p>
    <label for="text2">YouTube Id:</label>
    <input type="text" name="txtYouTubeGuidAdd" />

</p>
<p><input type="submit" name="btnAddYouTubeGuid" value="Add" /></p>
</fieldset>

<script type="text/javascript">
    function ShowEditDialog(galleryId, youTubeGuid) {
        $("[name=galleryId]").val(galleryId);
        $("[name=txtYouTubeGuidEdit]").val(youTubeGuid);
        $('#dlgEditYouTubeGuid').css('display', 'block');
    }

    $('#dlgEditYouTubeGuid').dialog();
</script>
